package edu.gyc.appshiro.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import edu.gyc.appshiro.dao.PermissionDao;
import edu.gyc.appshiro.model.*;
import edu.gyc.appshiro.dao.RoleDao;
import edu.gyc.appshiro.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gyc.appshiro.vo.PermissionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2021-02-02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {

    @Autowired
    PermissionDao permissionDao;

    @Autowired
    RolePermissionService rolePermissionService;

    @Autowired
    PermissionService permissionService;
//生成一个含有角色获取权限的状态acquireStatus的角色权限列表，角色获取了权限，值改为true
    private List<PermissionVo> getPermissionVoList(List<Integer> acquirePermissionIds){

        List<Permission> AllPermissions=permissionService.list();
        List<PermissionVo> permissionVos = new ArrayList<>();
        for (Permission permission : AllPermissions) {
            int n=permission.getId();
            PermissionVo permissionVo=new PermissionVo();
            BeanUtils.copyProperties(permission,permissionVo);

            if (acquirePermissionIds.contains(n)) {
                permissionVo.setAcquireStatus(true);
            }
            permissionVos.add(permissionVo);
        }
//让权限根据id号排序
        permissionVos.sort(new Comparator<PermissionVo>() {
            @Override
            public int compare(PermissionVo o1, PermissionVo o2) {
                int n=o1.getId();
                int m=o2.getId();
                if(n>m)
                    return 1;
                else if (n < m) {
                    return -1;
                }
                else {
                    return 0;
                }
            }
        });
        return permissionVos;
    }
    //根据角色id查询出该角色说获得的权限
    @Override
    public List<PermissionVo> getPermsByRoleId(Integer rid) {
        Set<Role> roles = new HashSet<>();
        //findPermissionsByRoleId 参数是若干个role，因一个用户可以拥有多个角色，这里就创建只有一个角色的集合，方便查询。
        Role role=new Role();
        role.setId(rid);
        roles.add(role);
        Set<Permission>  rolePermissions = permissionDao.findPermissionsByRoleId(roles);
        List<Integer> acquireIds=new ArrayList<>();
        for (Permission permission : rolePermissions) {
            acquireIds.add(permission.getId());
        }


        return getPermissionVoList(acquireIds);
    }
//更新角色权限，先删除该角色所有的原权限，再添加新的权限，需要使用事务处理
    @Override
    @Transactional
    public void updateRolePermissions(Integer roleId, List<Integer> ids) {
        if (ids.size() > 0) {
            rolePermissionService.remove(new LambdaUpdateWrapper<RolePermission>()
                    .eq(RolePermission::getRoleId,roleId));

            List<RolePermission> rolePermissions = new ArrayList<>();
            for (Integer id : ids) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(id);
                rolePermissions.add(rolePermission);
            }

            rolePermissionService.saveBatch(rolePermissions);
        }
    }


    @Override
    public List<PermissionVo> getUserPermissionByUid(Integer uid) {
        List<PermissionVo> permissionVos = permissionDao.getPermsByUid(uid);
        List<Integer> acquireIds=new ArrayList<>();
        for (Permission permission : permissionVos) {
            acquireIds.add(permission.getId());
        }
        return getPermissionVoList(acquireIds);
    }
@Autowired
    UserRoleService userRoleService;
    @Autowired
    RoleDao roleDao;
    @Autowired
    UserService userService;
    @Override
    public boolean addRoleUser(Integer rid, Integer uid) {
        List<User> users = roleDao.getUsersByRoleId(rid);
        User user = userService.getById(uid);
        for (User u : users) {
            if (u.getUsername().equals(user.getUsername())) {
                return false;
            }
        }
        UserRole userRole=new UserRole();
        userRole.setRoleId(rid);
        userRole.setUid(uid);
        boolean r = userRoleService.save(userRole);
        if (r) {
            return  true;
        }
        return false;
    }

    @Override
    public boolean addRole(Role role) {

        List<Role> roles=this.list();
        for (Role r : roles) {
            if (r.getRole().equals(role.getRole()) || r.getDescription().equals(role.getDescription())) {
                return false;
            }
        }
        save(role);
        return true;
    }

    @Override
    @Transactional
    public boolean delRole(Integer rid) {
        rolePermissionService.lambdaUpdate().eq(RolePermission::getRoleId,rid).remove();
        userRoleService.lambdaUpdate().eq(UserRole::getRoleId,rid).remove();
        this.removeById(rid);
        return true;
    }
}
